package hujinxi;

public class CalculateDate {
    public static String yesterDate(int year, int month, int day) {
        if (day > 1) {
            day--;
        } else {
            // 处理跨月的情况
            if (month == 1) {
                month = 12;
                year--;
            } else {
                month--;
            }
            day = getDaysInMonth(year, month);
        }
        return String.format("%d-%02d-%02d", year, month, day);
    }

    private static int getDaysInMonth(int year, int month) {
        switch (month) {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                return 31;
            case 4: case 6: case 9: case 11:
                return 30;
            case 2:
                return isLeapYear(year) ? 29 : 28;
            default:
                throw new IllegalArgumentException("Invalid month: " + month);
        }
    }

    private static boolean isLeapYear(int year) {
        if (year % 4 != 0) {
            return false;
        } else if (year % 100 != 0) {
            return true;
        } else {
            return year % 400 == 0;
        }
    }
}
